package com.purong.purong.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.purong.purong.R;
import com.purong.purong.bean.MsgBean;
import com.purong.purong.bean.NoticeBean;

import java.util.ArrayList;
import java.util.List;

/**
 * 系统公告
 */
public class SysMsgAdapter extends RecyclerView.Adapter<SysMsgAdapter.ViewHolder> {


    private List<MsgBean> itemList = new ArrayList<>();
    private OnMsgClickListener mListener;
    private Context mContext;
    public SysMsgAdapter(Context context) {
        this.mContext = context;
    }

    public void setData(List<MsgBean> noticeList) {
//        if (Utils.isEmpty(imageList)) {
//            return;
//        }
        this.itemList = noticeList;
    }
    public void setOnMsgClickListener(OnMsgClickListener listener){
        this.mListener=listener;
    }

    /**
     * 通过ViewHolder可以访问每个item的所有控件
     */
    public static class ViewHolder extends RecyclerView.ViewHolder {
        //绑定每个CircleItem的控件引用到ViewHolder
        private TextView date,content,title;
        private LinearLayout mainLay;
        private View topCutLine;
        public ViewHolder(View v) {
            super(v);
            date=(TextView)v.findViewById(R.id.date);
            title=(TextView)v.findViewById(R.id.title);
            content=(TextView)v.findViewById(R.id.content);
            mainLay=(LinearLayout)v.findViewById(R.id.main_lay);
            topCutLine=(View)v.findViewById(R.id.top_line);

        }
    }


    // 创建每个Item的视图 (invoked by the item_circle manager)
    @Override
    public SysMsgAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                       int viewType) {
        //创建circleItem所对应的控件
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.sys_msg_item, parent, false);
        // set the view's size, margins, paddings and item_circle parameters
        ViewHolder vh = new ViewHolder(v);

        return vh;
    }

    // Replace the contents of a view (invoked by the item_circle manager)
    @Override
    public void onBindViewHolder(final ViewHolder mHolder, final int position) {
        mHolder.date.setText(itemList.get(position).getCreate_time());
        mHolder.title.setText(itemList.get(position).getMessage_title());
        mHolder.content.setText(itemList.get(position).getMessage_content());
        mHolder.mainLay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener!=null)
                    mListener.onMsg(itemList.get(position));
            }
        });
        if (position==0){
            mHolder.topCutLine.setVisibility(View.VISIBLE);
        }else {
            mHolder.topCutLine.setVisibility(View.GONE);
        }
        if (itemList.get(position).getIs_read().equals("1")){
            mHolder.date.setTextColor(mContext.getResources().getColor(R.color.text_dark_gray));
            mHolder.title.setTextColor(mContext.getResources().getColor(R.color.text_dark_gray));
            mHolder.content.setTextColor(mContext.getResources().getColor(R.color.text_dark_gray));
        }else {
            mHolder.date.setTextColor(mContext.getResources().getColor(R.color.text_gray));
            mHolder.title.setTextColor(mContext.getResources().getColor(R.color.text_gray));
            mHolder.content.setTextColor(mContext.getResources().getColor(R.color.text_gray));
        }
    }



    // 获取Circle数据的item个数
    @Override
    public int getItemCount() {
        return itemList.size();
    }

    public interface OnMsgClickListener{
        void onMsg(MsgBean bean);
    }
}


